package com.ganxiang.ximenhong.config;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.widget.ImageView;

import com.ganxiang.ximenhong.R;
import com.ganxiang.ximenhong.model.CacheHandler;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;


public class ImageConfig {
	
	public static final int Style_Defaut = 0, Style_NoBg = 1, Style_Round = 2, Style_Avatar = 3, Style_CustomerRound = 4;
	
	private static DisplayImageOptions optionsDefaut;
	private static DisplayImageOptions optionsNoBg;
	private static DisplayImageOptions optionsRound;
	private static DisplayImageOptions optionsCustomerRound;
	

	@SuppressWarnings("deprecation")
	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY)
				.denyCacheImageMultipleSizesInMemory()
				.discCache(new UnlimitedDiscCache(CacheHandler.getInstance().getImageCacheDir(context)))
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.build();
		initDisplayImageOptions();
		ImageLoader.getInstance().init(config);
	}
	
	public static String getImgLocPath(String url){
		return ImageLoader.getInstance().getDiscCache().get(url).getPath();
	}

	public static void displayImage(String url, ImageView image){
		initDisplayImageOptions();
		ImageLoader.getInstance().displayImage(url, image, optionsDefaut);
	}
	
	public static void displayImage(String url, ImageView image, ImageLoadingListener l){
		initDisplayImageOptions();
		ImageLoader.getInstance().displayImage(url, image, optionsDefaut, l);
	}
	
	public static void displayImage(String url, ImageView image, DisplayImageOptions options, ImageLoadingListener l){
		if(options == null){
			ImageLoader.getInstance().displayImage(url, image, new DisplayImageOptions.Builder()
			.cacheInMemory(true)
			.cacheOnDisc(true).build(), l);
		}
		else{
			ImageLoader.getInstance().displayImage(url, image, options, l);
		}
	}
	
    public static void displayImage(String url, ImageView image, int optionStyle){
    	DisplayImageOptions options;
    	initDisplayImageOptions();
    	switch (optionStyle) {
		case Style_NoBg:
			options = optionsNoBg;
			break;
		case Style_Round:
			options = optionsRound;
			break;
		case Style_CustomerRound:
			options = optionsCustomerRound;
			break;
		default:
			options = optionsDefaut;
			break;
		}
    	ImageLoader.getInstance().displayImage(url, image, options);
    }
    
    public static void displayRoundsImage(String url, ImageView image, int rounds){
    	if(optionsCustomerRound == null){
    		optionsCustomerRound = new DisplayImageOptions.Builder()
			.displayer(new RoundedBitmapDisplayer(rounds))
			.showImageOnLoading(R.drawable.bg_default)
			.showImageForEmptyUri(R.drawable.bg_default)
			.showImageOnFail(R.drawable.bg_default)
			.resetViewBeforeLoading(true)
			.cacheInMemory(true)
			.cacheOnDisc(true).build();
		}
    	ImageLoader.getInstance().displayImage(url, image, optionsCustomerRound);
    }

	private static void initDisplayImageOptions(){
		if(optionsDefaut == null){
			optionsDefaut = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.bg_default)
			.showImageForEmptyUri(R.drawable.bg_default)
			.showImageOnFail(R.drawable.bg_default)
			.cacheInMemory(true)
			.cacheOnDisc(true).build();
		}
		
		if(optionsNoBg == null){
			optionsNoBg = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.nobg)
			.showImageForEmptyUri(R.drawable.nobg)
			.showImageOnFail(R.drawable.nobg)
			.cacheInMemory(true)
			.cacheOnDisc(true).build();
		}
		
		if(optionsRound == null){
			optionsRound = new DisplayImageOptions.Builder()
			/*.showImageOnLoading(R.drawable.default_pic)
			.showImageForEmptyUri(R.drawable.default_pic)
			.showImageOnFail(R.drawable.default_pic)*/
			.resetViewBeforeLoading(true)
			.displayer(new  RoundedBitmapDisplayer(15000))
			.cacheInMemory(true)
			.cacheOnDisc(true).build();
		}
		
	
	}
	
	/** 
	 * 获取圆角位图的方法 
	 * @param bitmap 需要转化成圆角的位图 
	 * @param pixels 圆角的度数，数值越大，圆角越大 
	 * @return 处理后的圆角位图 
	 */  
	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  
	    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
	    Canvas canvas = new Canvas(output);  
	    final int color = 0xff424242;  
	    final Paint paint = new Paint();  
	    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
	    final RectF rectF = new RectF(rect);  
	    final float roundPx = pixels;  
	    paint.setAntiAlias(true);  
	    canvas.drawARGB(0, 0, 0, 0);  
	    paint.setColor(color);  
	    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
	    canvas.drawBitmap(bitmap, rect, rect, paint);  
	    return output;  
	}  
}
